<template>
  <div class="app-container">
    <div class="index">
      <div class="searchHead">
        <el-button type="primary" @click="outerVisible = true">新增施肥信息</el-button>
        <el-input v-model="searchInput" placeholder="请输入要查询的数据" clearable class="searchIuput">
          <el-select slot="prepend" v-model="select" class="sel3" placeholder="请选择" style="width: 120px;">
            <el-option label="员工名字" value="employeeName" />
            <el-option label="肥料名称" value="fertilizerName" />
          </el-select>
          <el-button slot="append" v-model="searchInf" icon="el-icon-search" @click="searchMes" />
        </el-input>
      </div>
      <el-dialog title="新增施肥信息表" :visible.sync="outerVisible">
        <el-form class="form">
          <el-form-item class="item" label="种植批次" :label-width="formLabelWidth">
            <el-input v-model="addMes.plantId" class="in" autocomplete="off" />
          </el-form-item>
          <el-form-item class="item" label="地块号" :label-width="formLabelWidth">
            <el-input v-model="addMes.areaId" class="in" autocomplete="off" />
          </el-form-item>
          <el-form-item class="item" label="员工" :label-width="formLabelWidth">
            <el-select v-model="addMes.employeeId" class="sel2" placeholder="请选择" style="width: 186px;">
              <el-option v-for="item in allEmployee" :key="item.employee_id" :label="item.employee_name"
                :value="item.employee_id" />
            </el-select>
          </el-form-item>
          <div style="margin-left:52px">
            <label style="height: 36px;line-height: 36px;padding: 0 12px 0 0;">肥料名称</label>
            <el-select v-model="addMes.fertilizerId" class="sel2" placeholder="请选择" style="width: 186px;">
              <el-option v-for="item in allFur" :key="item.fertilizer_id" :label="item.fertilizer_name"
                :value="item.fertilizer_id" />
            </el-select>
          </div>
          <el-form-item class="item" label="施肥时间" :label-width="formLabelWidth">
            <el-date-picker v-model="addMes.useTime" type="datetime" class="in" autocomplete="off" placeholder="施肥时间"
              value-format="yyyy-MM-dd HH:mm:ss" format="yyyy-MM-dd HH:mm:ss" />
          </el-form-item>
          <el-form-item class="item" label="备注" :label-width="formLabelWidth">
            <el-input v-model="addMes.remark" class="in" autocomplete="off" />
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="outerVisible = false">取 消</el-button>
          <el-button type="primary" @click="addDri()">确 定</el-button>
        </div>
      </el-dialog>

      <el-table :data="mes" type="" style="width: 100%">
        <el-table-column prop="plantId" label="种植批次" />
        <el-table-column prop="areaId" label="地块号" />
        <el-table-column prop="employeeName" label="员工姓名" />
        <el-table-column prop="fertilizerName" label="肥料名称" />
        <el-table-column prop="useTime" label="施肥时间" width="103" />
        <el-table-column prop="remark" label="备注" />
        <el-table-column fixed="right" label="操作" width="100">
          <template slot-scope="scope">
            <el-button type="text" @click="changeMsg(mes[scope.$index],scope.$index)">修改</el-button>
            <el-button type="text" style="color:red" @click="mes[scope.$index].isShowDel = true">删除</el-button>

            <el-dialog :modal-append-to-body="false" title="提示" :visible.sync="mes[scope.$index].isShowDel" width="30%">
              <span>确认要删除此条信息吗？</span>
              <span slot="footer" class="dialog-footer">
                <el-button @click="mes[scope.$index].isShowDel = false">取 消</el-button>
                <el-button type="primary" @click="driveDel(scope.$index)">确 定</el-button>
              </span>
            </el-dialog>

            <el-dialog :modal-append-to-body="false" title="修改信息" :visible.sync="mes[scope.$index].isShow"
              :before-close="handleClose">
              <el-form class="form">
                <el-form-item class="item" label="种植批次" :label-width="formLabelWidth">
                  <el-input v-model="mes[scope.$index].plantId" class="in" autocomplete="off" disabled/>
                </el-form-item>
                <el-form-item class="item" label="地块号" :label-width="formLabelWidth">
                  <el-input v-model="mes[scope.$index].areaId" class="in" autocomplete="off" disabled/>
                </el-form-item>
                <el-form-item class="item" label="员工姓名" :label-width="formLabelWidth">
                  <el-select v-model="mes[scope.$index].employeeId" class="sel2" placeholder="请选择"
                    style="width: 186px;">
                    <el-option v-for="item in allEmployee" :key="item.employee_id" :label="item.employee_name"
                      :value="item.employee_id" />
                  </el-select>
                </el-form-item>
                <div style="margin-left:52px">
                  <label style="height: 36px;line-height: 36px;padding: 0 12px 0 0;">肥料名称</label>
                  <el-select v-model="mes[scope.$index].fertilizerId" class="sel2" placeholder="请选择"
                    style="width: 186px;">
                    <el-option v-for="item in allFur" :key="item.fertilizer_id" :label="item.fertilizer_name"
                      :value="item.fertilizer_id" />
                  </el-select>
                </div>
                <el-form-item class="item" label="施肥时间" :label-width="formLabelWidth">
                  <el-date-picker v-model="mes[scope.$index].useTime" type="datetime" class="in" autocomplete="off"
                    placeholder="施肥时间" value-format="yyyy-MM-dd HH:mm:ss" format="yyyy-MM-dd HH:mm:ss" />
                </el-form-item>
                <el-form-item class="item" label="备注" :label-width="formLabelWidth">
                  <el-input v-model="mes[scope.$index].remark" autocomplete="off" type="textarea"
                    style="width: 350px;" />
                </el-form-item>
              </el-form>

              <div slot="footer" class="dialog-footer">
                <el-button @click="cancelChangeMsg(scope.$index)">取 消</el-button>
                <el-button type="primary" @click="changeInspectionForm(scope.$index,mes[scope.$index])">确 定</el-button>
              </div>

            </el-dialog>
          </template>
        </el-table-column>
      </el-table>
      <div class="page">
        <el-pagination :page-size="5" layout="prev, pager, next, jumper" :total="total" :current-page="pageNum"
          @current-change="handleCurrentChange" />
      </div>
    </div>
  </div>
</template>

<script>
import request from '../../../utils/request'
export default {
  name: 'FarmlandSelection',
  data () {
    return {
      mes: [],
      firstMes: [],
      allMes: [],
      index: null,
      dialogTableVisible: false,
      formLabelWidth: '120px',
      addMes: {},
      dialogFormVisible: false,
      outerVisible: false,
      pageSize: 5,
      pageNum: 1,
      deliverPageNum: 1,
      deliverPageSize: null,
      select: '',
      searchInf: '',
      searchInput: '',
      total: 0,
      allEmployee: [],
      allProduct: [],
      allCompany: [],
      allFur: [],
      searchForm: {}
    }
  },
  async mounted () {
    this.load()
    this.getEmployee()
    this.getProduct()
    this.getFur()
  },
  methods: {
    async load () {
      await request({
        method: 'post',
        url: `/plant/fuse/${this.pageNum}/${this.pageSize}`,
        data: this.searchForm
      }).then((response) => {
        this.mes = response.data.records
        for (let i = 0; i < this.mes.length; i++) {
          this.$set(this.mes[i], 'isShow', false)
          this.$set(this.mes[i], 'isShowDel', false)
        }
        this.total = response.data.total
      })
    },
    async getEmployee () {
      await request({
        method: 'get',
        url: '/plant/employee/getAll'
      }).then((response) => {
        this.allEmployee = JSON.parse(JSON.stringify(response.data))
      })
    },
    async getProduct () {
      await request({
        method: 'get',
        url: '/plant/product/getAll'
      }).then((response) => {
        this.allProduct = JSON.parse(JSON.stringify(response.data))
      })
    },
    async getFur () {
      await request({
        method: 'get',
        url: '/plant/fer/getAll'
      }).then((response) => {
        this.allFur = JSON.parse(JSON.stringify(response.data))
        console.log(response, '123123')
      })
    },
    changeMsg (mesIndex, index) {
      mesIndex.isShow = true
      this.firstMes = JSON.parse(JSON.stringify(this.mes))[index]
      this.index = index
    },

    async changeInspectionForm (index, mes) {
      var changMes = JSON.parse(JSON.stringify(this.mes))[index]
      for (let i = 0; i < this.allEmployee.length; i++) {
        const e = this.allEmployee[i]
        if (e.employee_id == changMes.employeeId) {
          changMes.employeeName = e.employee_name
        }
      }
      const newChangMes = {
        id: changMes.id,
        plantId: changMes.plantId,
        areaId: changMes.areaId,
        employeeId: changMes.employeeId,
        employeeName: changMes.employeeName,
        fertilizerId: changMes.fertilizerId,
        fertilizerName: changMes.fertilizerName,
        useTime: changMes.useTime,
        productId: changMes.productId,
        productName: changMes.productName,
        remark: changMes.remark
      }
      await request({
        method: 'put',
        url: '/plant/fuse/' + mes.id,
        data: newChangMes
      }).then(response => {
        this.load()
        if (response.statusCode == 20000) {
          this.$message({
            message: '修改成功',
            type: 'success'
          })
        } else {
          this.$message({
            message: '发生错误，修改失败',
            type: 'warning'
          })
        }
        this.mes[index].isShow = false
      })
    },
    cancelChangeMsg (index) {
      this.mes[index].isShow = false
      this.mes[index] = JSON.parse(JSON.stringify(this.firstMes))
      this.load()
    },
    async driveDel (index) {
      await request({
        method: 'delete',
        url: '/plant/fuse/' + this.mes[index].id
      }).then(response => {
        if (response.statusCode == 20000) {
          this.dialogFormVisible = false
          this.load()
          this.$message({
            message: '删除成功',
            type: 'success'
          })
        } else {
          this.$message({
            message: '发生错误，删除失败',
            type: 'warning'
          })
        }
      })
      this.mes[index].isShowDel = false
    },

    async addDri () {
      console.log('this.addMes', this.addMes)
      for (let i = 0; i < this.allProduct.length; i++) {
        const e = this.allProduct[i]
        if (e.product_id == this.addMes.productId) {
          this.addMes.productName = e.product_name
        }
      }
      for (let i = 0; i < this.allFur.length; i++) {
        const e = this.allFur[i]
        if (e.fertilizer_id == this.addMes.fertilizerId) {
          this.addMes.fertilizerName = e.fertilizer_name
        }
      }
      for (let i = 0; i < this.allEmployee.length; i++) {
        const e = this.allEmployee[i]
        if (e.employee_id == this.addMes.employeeId) {
          this.addMes.employeeName = e.employee_name
          console.log('this.addMes-allEmployee', this.addMes)
        }
      }
      request({
        method: 'post',
        url: '/plant/fuse',
        data: this.addMes
      }).then(response => {
        if (response.statusCode == 20000) {
          this.addMes = {}
          this.load()
          this.outerVisible = false;
          this.$message({
            message: '添加成功',
            type: 'success'
          })
        } else {
          this.$message({
            message: '发生错误，添加失败',
            type: 'warning'
          })
        }
      })
    },
    handleCurrentChange (val) {
      this.pageNum = val;
      this.load()
    },

    searchMes () {
      if (this.searchInput && this.select) {
        if (this.select == 'productName') {
          this.searchForm.productName = this.searchInput
        } else if (this.select == 'employeeName') {
          this.searchForm.employeeName = this.searchInput
        } else if (this.select == 'fertilizerName') {
          this.searchForm.fertilizerName = this.searchInput
        }
      } else {
        this.searchForm = {}
      }
      this.load()
    },

    handleClose (done) {
      this.$confirm('确认关闭？')
        .then(_ => {
          done()
          this.addMes = {}
          this.cancelChangeMsg(this.index)
        })
        .catch(_ => { })
    }
  }
}
</script>

<style lang="scss" scoped>
.header {
  width: 100%;
  height: 350px;
  // border: 1px solid black;
  display: flex;
  margin-bottom: 20px;
  justify-content: space-between;
}

.form {
  display: flex;
  flex-wrap: wrap;
}

.sel {
  width: 360px;
  display: flex;
  height: 50px;
  justify-content: flex-end;
  align-content: center;
}

.sel2 {
  width: 240px;
}

.page {
  width: 100%;
  display: flex;
  justify-content: right;
}

.imgDri {
  height: 300px;
  width: 400px;
}

.w1 {
  height: 80px;
  font-size: 25px;
  font-weight: 350;
}

.w2 {
  height: 50px;
  font-size: 15px;
  font-weight: 250;
}

.searchHead {
  display: flex;
  justify-content: space-between;
}

.searchIuput {
  width: 350px;
}

.sel3 {
  width: 120px;
}
</style>

